package net.lab1024.sa.admin.module.business.erp.mmo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.inv.dao.InventordDao;
import net.lab1024.sa.admin.module.business.erp.inv.domain.entity.InventordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrqordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrqordaDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrqordbDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordbEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.*;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MrqordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MrqordaVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 生产领料单 Service
 *
 * @Author AKITA
 * @Date 2025-08-18 10:37:56
 * @Copyright AKITA
 */

@Service
public class MrqordService {

    @Resource
    private MrqordDao mrqordDao;
    @Resource
    private MrqordaDao mrqordaDao;
    @Resource
    private InventordDao inventordDao;
    @Resource
    private MrqordbDao mrqordbDao;

    /**
     * 分页查询
     */
    public PageResult<MrqordVO> queryPage(MrqordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MrqordVO> list = mrqordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */

    @Transactional
    public ResponseDTO<Long> add(MrqordAddForm addForm) {
        MrqordEntity mrqordEntity = SmartBeanUtil.copy(addForm, MrqordEntity.class);
        mrqordDao.insert(mrqordEntity);

        int lineNum = 1;

        for (MrqordaAddForm mrqordaAddForm : addForm.getLista()) {
            MrqordaEntity copy = SmartBeanUtil.copy(mrqordaAddForm, MrqordaEntity.class);
            copy.setId(mrqordEntity.getId());
            mrqordaDao.insert(copy);
            for (MrqordbAddForm mrqordbAddForm : mrqordaAddForm.getLista()) {
                MrqordbEntity copy1 = SmartBeanUtil.copy(mrqordbAddForm, MrqordbEntity.class);
                copy1.setId(mrqordEntity.getId());
                copy1.setLineNum(lineNum++);
                copy1.setBaseId(mrqordEntity.getId());
                copy1.setBaseLineNum(copy.getLineNum());
                /*
                 * 1、校验当前批次物料是否
                 *
                 *
                 *
                 * */

                InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbAddForm.getItemId()).eq(InventordEntity::getBatchId, mrqordbAddForm.getBatchId()));
                if (inventordEntity.getQty().compareTo(mrqordbAddForm.getQty()) >= 0) {
                    inventordEntity.setQty(inventordEntity.getQty().subtract(mrqordbAddForm.getQty()));
                    inventordDao.update(inventordEntity, new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbAddForm.getItemId()).eq(InventordEntity::getBatchId, mrqordbAddForm.getBatchId()));
                }


                mrqordbDao.insert(copy1);
            }

        }


        return ResponseDTO.ok(mrqordEntity.getId());
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(MrqordUpdateForm updateForm) {
        MrqordEntity mrqordEntity = SmartBeanUtil.copy(updateForm, MrqordEntity.class);
        mrqordDao.updateById(mrqordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        mrqordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> delete(Long id) {
        List<MrqordbEntity> mrqordbEntities = mrqordbDao.selectList(new LambdaQueryWrapper<MrqordbEntity>().eq(MrqordbEntity::getId, id));
        for (MrqordbEntity mrqordbEntity : mrqordbEntities) {
            InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbEntity.getItemId()).eq(InventordEntity::getBatchId, mrqordbEntity.getBatchId()));
            inventordEntity.setQty(inventordEntity.getQty().add(mrqordbEntity.getQty()));
            inventordDao.update(inventordEntity, new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbEntity.getItemId()).eq(InventordEntity::getBatchId, mrqordbEntity.getBatchId()));



        }
        mrqordDao.deleteById(id);
        mrqordaDao.deleteById(id);
        mrqordbDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<MrqordVO> info(Long id) {
        MrqordEntity mrqordEntity = mrqordDao.selectById(id);
        MrqordVO copy = SmartBeanUtil.copy(mrqordEntity, MrqordVO.class);


        List<MrqordaVO> mrqordaVOS = mrqordaDao.selectLista(id);
        copy.setLista(mrqordaVOS);
        return ResponseDTO.ok(copy);


    }

    public ResponseDTO<MrqordVO> infoUnexecute(Long id) {
        List<MrqordVO> poordVOS = mrqordDao.infoUnexecute(id);
        return ResponseDTO.ok(poordVOS.get(0));

    }
}
